package com.cc.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import com.cc.web.vo.AmountVo;
import com.cc.pojo.dto.AmountDto;
import com.cc.service.amount.AmountService;

/**
 * @author c.c
 * @Description 控制层
 * @since 2023/3/31
 */
@Api(tags = "(用户自己定义)")
@Slf4j
@RestController
@RequestMapping("/api/（用户自己定义）")
public class AmountController {
  @Resource
  private AmountService amountService;

  /**
   * 分页
   *
   * @author C.c
   **/
  @ApiOperation("清单")
  @GetMapping("/list")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "name", value = "待定"),
    @ApiImplicitParam(name = "page", value = "第几页"),
    @ApiImplicitParam(name = "pageSize", value = "页面大小")
  })
  public AjaxReturnBean<List<AmountVo>> list(@RequestParam(name = "page", defaultValue = "1", required = false) int pageNum,
                                             @RequestParam(name = "pageSize", defaultValue = "15", required = false) int pageSize,
                                             @RequestParam(name = "name", required = false) String name) {
    Page<AmountVo> amountVoVos = amountService.query(name, pageNum, pageSize);
    return AjaxReturnBean.createSuccess("查询成功", amountVoVos.getContent(), amountVoVos.getTotalElements());
  }

  @ApiOperation("添加")
  @PostMapping("/add")
  public AjaxReturnBean<Boolean> add(@RequestBody AmountDto amountDto) {
    if (amountService.add(amountDto)) {
      return AjaxReturnBean.createSuccess("保存成功");
    }
    return AjaxReturnBean.createError("保存失败");
  }


  @ApiOperation("删除设备")
  @DeleteMapping("/delete/{id}")
  public AjaxReturnBean<Boolean> delete(@PathVariable String id) {
    if (amountService.delete(id)) {
      return AjaxReturnBean.createSuccess("删除成功");
    }
    return AjaxReturnBean.createError("删除失败");
  }


  @ApiOperation("编辑设备")
  @PutMapping("/edit")
  public AjaxReturnBean<Boolean> edit(@RequestBody AmountDto amountDto) {
    if (amountService.edit(equipmentDto)) {
      return AjaxReturnBean.createSuccess("编辑成功");
    }
    return AjaxReturnBean.createError("编辑失败");
  }


  @ApiOperation("查看设备")
  @GetMapping("/view/{id}")
  public AjaxReturnBean<AmountVo> viewSingle(@PathVariable("id") String id) {
    AmountVo amountVoVos = amountVoVos.view(id);
    return AjaxReturnBean.createSuccess(equipmentVo);
  }
}